package com.bankcomm.voice;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.bankcomm.R;
import com.xioai.framework.api.jsonbean.Response;
import com.xioai.framework.common.spellindex.contacts.ContactsSearch;
import com.xioai.framework.common.spellindex.contacts.ContactsSearchOld;
import com.xioai.framework.common.spellindex.contacts.PhoneInfoEntity;
import com.xioai.framework.common.spellindex.contacts.PhoneNumSearchResults;
import com.xioai.framework.voicecontrol.VoiceControl;

public class TalkContacts extends LinearLayout {
	private LinearLayout phoneLayout;
 
	private VoiceControl vc;
    
	public TalkContacts(Context context, final VoiceControl vc, final Response response) {
		super(context);
		this.vc = vc;
		this.setOrientation(LinearLayout.VERTICAL);
		final List<PhoneInfoEntity> contacts=new ArrayList<PhoneInfoEntity>();
		String numberFilter="";
		if(response.getPlugin().getArgs().size()>3)
		{   	//判断是否有号码过滤器
			numberFilter=response.getPlugin().getArgs().get(3);
		}
		int sdk = 4;
		try {
			sdk = Integer.parseInt(android.os.Build.VERSION.SDK);
		} catch (Exception e) {
			// TODO: handle exception
		}
		PhoneNumSearchResults pnsr = null;
		pnsr = ContactsSearch.getInstance(context).searchPhone(response.getPlugin().getArgs().get(0),numberFilter);
		
		if(sdk >= 6){
			pnsr = ContactsSearch.getInstance(context).searchPhone(response.getPlugin().getArgs().get(0),numberFilter);
		}else{
			pnsr = ContactsSearchOld.getInstance(context).searchPhone(response.getPlugin().getArgs().get(0),numberFilter);
		}
		
		//号码“-”过滤
		for (PhoneInfoEntity info: pnsr.getPhones()) {     
			PhoneInfoEntity pie=new PhoneInfoEntity();
			if(info.getName()!=null)
			{
			pie.setName(info.getName().replaceAll("-", ""));
			}
			if(info.getPhoneNum()!=null)
			{
			pie.setPhoneNum(info.getPhoneNum().replaceAll("-", ""));
			}
			contacts.add(pie);
		}

		phoneLayout = (LinearLayout) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.xiaoi_talk_contacts, null);
		LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(-1,-1);
		LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(-1,-2);
		
      if(contacts.size()<=0)
      {
    	      String serviceResult=response.getPlugin().getArgs().get(1);
		      serviceResult = serviceResult.replace("{0}", "");
		      serviceResult = serviceResult.replace("{1}", "");
			   vc.askData(serviceResult);
      }else
      {
		for (int i = 0; i < contacts.size(); i++) {
			final int j = i;
			Button btn = new Button(context);
			btn.setText(contacts.get(i).getName() + ":"+ contacts.get(i).getPhoneNum());
			btn.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
			btn.setTextSize(20);
			btn.setTextColor(Color.WHITE);
			btn.setBackgroundColor(Color.TRANSPARENT);
			btn.setOnClickListener(new OnClickListener() { 
				@Override
				public void onClick(View v) {
				    String name=contacts.get(j).getName();
				    String number=contacts.get(j).getPhoneNum();
				    String serviceResult=response.getPlugin().getArgs().get(1);
				    serviceResult = serviceResult.replace("{0}", number);
				    serviceResult = serviceResult.replace("{1}", name);
					vc.askData(serviceResult);
				}
			});
			phoneLayout.addView(btn, btnParams);

			View vImg = new View(context);
			vImg.setBackgroundResource(R.drawable.xiaoi_line_bg);
			vImg.setLayoutParams(imgParams);

			if (i < pnsr.getPhones().size() - 1) {
				phoneLayout.addView(vImg);
			}
			
		}
		 
		this.addView(phoneLayout, new LinearLayout.LayoutParams(-1, -2));
      }
	}

}
